package com.py.credit.service.impl;

import com.py.credit.dao.LhjcApiDao;
import com.py.credit.dao.PluginApiDao;
import com.py.credit.service.LhjcApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LhjcApiServiceImpl implements LhjcApiService {

    @Autowired
    private LhjcApiDao lhjcApiDao;

    @Autowired
    private PluginApiDao pluginApiDao;

    @Override
    public Map<String, Object> getXzspDetail(String name, String bm, String ajbh, String jcType, String ztType, String ipAddress, long userId) {
        Map<String, Object> map = new HashMap<String, Object>();

        List<Map<String, String>> gzList = lhjcApiDao.selectXzspGzList(name, bm, ajbh, jcType, ztType);
        if (null == gzList || gzList.size() == 0) {
            map.put("code", 1);
            map.put("msg", "暂无该信用主体相关的联合奖惩数据");
            return map;
        }

        List<Map<String, String>> csList = lhjcApiDao.selectXzspCsList(name, bm, ajbh, jcType, ztType);
        if (null == csList || csList.size() == 0) {
            map.put("code", 3);
            map.put("msg", "无对应的部门配合执行的措施");
            return map;
        }

        List<Map<String, String>> ajbhList = lhjcApiDao.selectXzspAjbhList(name, bm, ajbh, jcType, ztType);

        //拼接事件编号和措施
        for (Map<String, String> cs : csList
                ) {
            cs.put("measure", cs.get("id") + "-" + lhjcApiDao.selectAjbhByCsIdAndQymc(cs.get("id"), name, bm, ajbh, jcType, ztType));
        }

        Map<String, Object> data = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        data.put("gzList", gzList);
        data.put("csList", csList);
        data.put("ajbhList", ajbhList);
        map.put("data", data);

        pluginApiDao.insertApiLog(userId, name, bm, ipAddress);

        return map;
    }

    @Override
    public Map<String, Object> getDetail(String loginName, String name, String bm, String ajbh, String jcType, String ztType, String ipAddress, long userId, long deptId) {
        Map<String, Object> map = new HashMap<String, Object>();

        List<Map<String, String>> gzList = lhjcApiDao.selectGzList(deptId, name, bm, ajbh, jcType, ztType);
        if (null == gzList || gzList.size() == 0) {
            map.put("code", 1);
            map.put("msg", "暂无该信用主体相关的联合奖惩数据");
            return map;
        }

        List<Map<String, String>> csList = lhjcApiDao.selectCsList(deptId, name, bm, ajbh, jcType, ztType);
        if (null == csList || csList.size() == 0) {
            map.put("code", 3);
            map.put("msg", "无对应的部门配合执行的措施");
            return map;
        }

        List<Map<String, String>> ajbhList = lhjcApiDao.selectAjbhList(deptId, name, bm, ajbh, jcType, ztType);

        //拼接事件编号和措施
        for (Map<String, String> cs : csList
                ) {
            cs.put("measure", cs.get("id") + "-" + lhjcApiDao.selectAjbhByCsIdAndQymc(cs.get("id"), name, bm, ajbh, jcType, ztType));
        }

        Map<String, Object> data = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        data.put("gzList", gzList);
        data.put("csList", csList);
        data.put("ajbhList", ajbhList);
        map.put("data", data);

        pluginApiDao.insertApiLog(userId, name, bm, ipAddress);

        return map;
    }

    @Override
    public int getHongHeimdCount(String name, String bm, String type, String jclb, String ipAddress, long userId) {
        int hongHeimdCount = lhjcApiDao.getHongHeimdCount(name, bm, type, jclb);
        pluginApiDao.insertApiLog(userId, name, bm, ipAddress);
        return hongHeimdCount;
    }
}
